#ifndef GAMETD_ANIMATION_H
#define GAMETD_ANIMATION_H

#include <SDL.h>
#include <utility>
#include <vector>
#include <functional>
#include "Timer.h"

class Animation {
public:
    typedef std::function<void()> PlayCallback;

    Animation();
    ~Animation() = default;
    void Reset();
    void SetFrameData(SDL_Texture *srcTexture, int num_h, int num_v, const std::vector<int> &idx_list);
    void SetLoop(bool flag);
    void SetInterval(double interval);
    void SetOnFinished(PlayCallback playCallback);
    void OnUpdate(double delta);
    void OnRender(SDL_Renderer *renderer, const SDL_Point &pos_dst, double angle = 0) const;

private:
    Timer timer;
    bool isLoop = true;                     // 是否循环播放
    size_t idxFrame = 0;                    // 当前的桢索引
    PlayCallback onFinished;                // 动画结束的事件
    SDL_Texture *texture = nullptr;         // 实际动画所使用的纹理
    std::vector<SDL_Rect> rectSrcList;      // 裁剪矩形的数组
    int widthFrame = 0, heightFrame = 0;    // 记录动画的宽度和高度
};


#endif //GAMETD_ANIMATION_H
